from django.db import models

# Create your models here.
from utils.models import BaseModel


class SKU(BaseModel):
    """商品SKU"""
    name = models.CharField(max_length=50, verbose_name='名称')
    caption = models.CharField(max_length=100, verbose_name='副标题')
    # category_id====>category对象的id
    price = models.DecimalField(max_digits=10, decimal_places=2,
                                verbose_name='单价')
    cost_price = models.DecimalField(max_digits=10, decimal_places=2,
                                     verbose_name='进价')
    market_price = models.DecimalField(max_digits=10, decimal_places=2,
                                       verbose_name='市场价')
    stock = models.IntegerField(default=0, verbose_name='库存')
    sales = models.IntegerField(default=0, verbose_name='销量')
    comments = models.IntegerField(default=0, verbose_name='评价数')
    is_launched = models.BooleanField(default=True, verbose_name='是否上架销售')
    default_image = models.ImageField(max_length=200, default='', null=True,
                                      blank=True, verbose_name='默认图片')

    class Meta:
        db_table = 'tb_sku'
        verbose_name = '商品SKU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.name)


class Category(BaseModel):
    """商品分类表"""
    name = models.CharField(max_length=120, verbose_name="分类名称")


class VisitCategory(models.Model):
    """
    用户访问商品分类时间表
    """
    category = models.ForeignKey(to=Category, on_delete=models.CASCADE,
                                 related_name='visit_category')
    visit_time = models.DateTimeField(auto_now_add=True, verbose_name="访问时间")


class ImageFile(BaseModel):
    """
    图片模型
    图片数据存在其他存储器中，该模型主要存储数据的url
    """
    url = models.CharField(max_length=120, verbose_name="图片url地址")
    desc = models.TextField(default="", verbose_name="文件详情描述")
    f_sku = models.OneToOneField(to=SKU, on_delete=models.CASCADE,
                              related_name='image_file')
